﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace WebTranslator {
	class TranslationServer : ClientProtocol {
		int myPort;
		public int activeUsers = 0;
		CTSProtocolS listenerToUsers;
		public Dictionary<string, ClientTSConnection> connections = new Dictionary<string, ClientTSConnection>();
		public TranslationServer(IPAddress masterIP, int port) {
			myPort = port;
			this.client = new TcpClient(masterIP+"", port);
			SetSockets(client.Client);
			listenerToUsers = new CTSProtocolS(this);
			listenerToUsers.StartListener(port + StartingApplicationPorts.NumberOfTranslationServers);
			new Thread(listenerToUsers.Listen).Start();
			this.Send("TS " + StartingApplicationPorts.ChosenAddress);
			Console.WriteLine(port + this.Receive());
			new Thread(this.Listener).Start();
		}

		public void Listener() {
			string msg;
			string[] napisy;
			while (true) {
				msg = Receive();
				switch (msg) {
					default:
						napisy = msg.Split(new char[] { ' ' }, 2);
						connections[napisy[0]].Send(napisy[1]);
						break;
				}
			}
		}
	}
}
